package com.servlet.project.controller;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializer;
import com.servlet.project.entity.Activity;
import com.servlet.project.entity.Competition;
import com.servlet.project.service.ActivityService;
import com.servlet.project.service.CompetitionService;
import com.servlet.project.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.*;

@WebServlet(name = "CompetitionServlet",urlPatterns = "/CompetitionServlet")
public class CompetitionServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method=request.getParameter("method");
        request.setCharacterEncoding("utf-8");
        CompetitionService service=new CompetitionService();
        if(method.equals("list")){//列表
            response.setContentType("text/json;charset=utf-8");
            String competitionname=request.getParameter("competitionname");
            String pageStr=request.getParameter("page");
            String rowsStr=request.getParameter("rows");
            int page=1;
            int rows=10;
            if(pageStr!=null&&!pageStr.equals("")){
                page=Integer.parseInt(pageStr);
            }
            if(rowsStr!=null&&!rowsStr.equals("")){
                rows=Integer.parseInt(rowsStr);
            }
            Competition obj=new Competition();
            obj.setCompetitionname(competitionname);
            //获取当前总数
            int dataCount=service.getListCount(obj);
            //获取分页后的列表数据
            List<Competition> memberList=service.getListAll((page-1)*rows,rows,obj);
            PrintWriter writer = response.getWriter();
            Map<String,Object> objectMap=new HashMap<>();
            objectMap.put("count",dataCount);
            objectMap.put("data",memberList);
            // 创建一个日期格式化对象，设置为中文
            SimpleDateFormat chineseDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINESE);
            // 创建一个Gson对象，设置日期格式化
            Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").setPrettyPrinting()
                    .registerTypeAdapter(Date.class, (JsonSerializer<Date>) (src, typeOfSrc, context) -> new JsonPrimitive(chineseDateFormat.format(src)))
                    .create();
            writer.write(gson.toJson(Result.reOk("ok",objectMap)));
        }else if(method.equals("add")){//新增
            response.setContentType("text/json;charset=utf-8");
            String competitionname=request.getParameter("competitionname");
            String competitiondate=request.getParameter("competitiondate");
            String competitionlocation=request.getParameter("competitionlocation");
            String competitiontype=request.getParameter("competitiontype");
            String competitionarranger=request.getParameter("competitionarranger");
            String competitionjiang=request.getParameter("competitionjiang");
            List<Object> objects=new ArrayList<>();
            objects.add(competitionname);
            objects.add(competitiondate);
            objects.add(competitionlocation);
            objects.add(competitiontype);
            objects.add(competitionarranger);
            objects.add(competitionjiang);
            int count=service.add(objects);
            //将当前用户信息转化为json传回给前端（用的JSON的转化方法）
            PrintWriter writer = response.getWriter();
            if(count==1){//注册成功
                writer.write(new Gson().toJson(Result.reOk("修改成功")));
            }else{//注册失败
                writer.write(new Gson().toJson(Result.reError("修改失败")));
            }
        }else if(method.equals("up")){//修改
            response.setContentType("text/json;charset=utf-8");
            String competitionname=request.getParameter("competitionname");
            String competitiondate=request.getParameter("competitiondate");
            String competitionlocation=request.getParameter("competitionlocation");
            String competitiontype=request.getParameter("competitiontype");
            String competitionarranger=request.getParameter("competitionarranger");
            String competitionjiang=request.getParameter("competitionjiang");
            String competitionid=request.getParameter("competitionid");
            List<Object> objects=new ArrayList<>();
            objects.add(competitionname);
            objects.add(competitiondate);
            objects.add(competitionlocation);
            objects.add(competitiontype);
            objects.add(competitionarranger);
            objects.add(competitionjiang);
            objects.add(competitionid);
            int count=service.up(objects);
            //将当前用户信息转化为json传回给前端（用的JSON的转化方法）
            PrintWriter writer = response.getWriter();
            if(count==1){//注册成功
                writer.write(new Gson().toJson(Result.reOk("修改成功")));
            }else{//注册失败
                writer.write(new Gson().toJson(Result.reError("修改失败")));
            }
        }else if(method.equals("get")){//获取当前信息
            response.setContentType("text/json;charset=utf-8");
            String competitionid=request.getParameter("competitionid");
            Competition member=service.getId(competitionid);
            //将当前用户信息转化为json传回给前端（用的JSON的转化方法）
            PrintWriter writer = response.getWriter();
            // 创建一个日期格式化对象，设置为中文
            SimpleDateFormat chineseDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINESE);
            // 创建一个Gson对象，设置日期格式化
            Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").setPrettyPrinting()
                    .registerTypeAdapter(Date.class, (JsonSerializer<Date>) (src, typeOfSrc, context) -> new JsonPrimitive(chineseDateFormat.format(src)))
                    .create();
            writer.write(gson.toJson(Result.reOk("ok",member)));
        }else if(method.equals("del")){//删除
            response.setContentType("text/json;charset=utf-8");
            String activityid=request.getParameter("competitionid");
            List<Object> objects=new ArrayList<>();
            objects.add(activityid);
            int count=service.del(objects);
            //将当前用户信息转化为json传回给前端（用的JSON的转化方法）
            PrintWriter writer = response.getWriter();
            if(count==1){//删除成功
                writer.write(new Gson().toJson(Result.reOk("删除成功")));
            }else{//删除失败
                writer.write(new Gson().toJson(Result.reError("删除失败")));
            }
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }


}
